package com.yaorange.nongjt.certificate.service.impl;

import java.util.Date;
import java.util.List;

import com.yaorange.nongjt.certificate.bo.RegisterBo;
import com.yaorange.nongjt.certificate.domain.NjtUserPerfect;
import com.yaorange.nongjt.certificate.mapper.NjtUserPerfectMapper;
import com.yaorange.nongjt.common.constant.NjtConstants;
import com.yaorange.nongjt.common.core.domain.entity.SysUser;
import com.yaorange.nongjt.common.enums.YesOrNoEnum;
import com.yaorange.nongjt.common.exception.ServiceException;
import com.yaorange.nongjt.common.utils.DateUtils;
import com.yaorange.nongjt.common.utils.SecurityUtils;
import com.yaorange.nongjt.common.utils.StringUtils;
import com.yaorange.nongjt.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.yaorange.nongjt.certificate.mapper.NjtUserRegisterMapper;
import com.yaorange.nongjt.certificate.domain.NjtUserRegister;
import com.yaorange.nongjt.certificate.service.INjtUserRegisterService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户注册信息Service业务层处理
 *
 * @author coach tam
 * @date 2024-10-12
 */
@Slf4j
@Service
public class NjtUserRegisterServiceImpl implements INjtUserRegisterService
{
    @Autowired
    private NjtUserRegisterMapper njtUserRegisterMapper;
    @Autowired
    private NjtUserPerfectMapper njtUserPerfectMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询用户注册信息
     *
     * @param id 用户注册信息主键
     * @return 用户注册信息
     */
    @Override
    public NjtUserRegister selectNjtUserRegisterById(Long id)
    {
        return njtUserRegisterMapper.selectNjtUserRegisterById(id);
    }

    /**
     * 查询用户注册信息列表
     *
     * @param njtUserRegister 用户注册信息
     * @return 用户注册信息
     */
    @Override
    public List<NjtUserRegister> selectNjtUserRegisterList(NjtUserRegister njtUserRegister)
    {
        return njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister);
    }

    /**
     * 新增用户注册信息
     *
     * @param njtUserRegister 用户注册信息
     * @return 结果
     */
    @Override
    public int insertNjtUserRegister(NjtUserRegister njtUserRegister)
    {
        njtUserRegister.setCreateTime(DateUtils.getNowDate());
        return njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }

    /**
     * 修改用户注册信息
     *
     * @param njtUserRegister 用户注册信息
     * @return 结果
     */
    @Override
    public int updateNjtUserRegister(NjtUserRegister njtUserRegister)
    {
        njtUserRegister.setUpdateTime(DateUtils.getNowDate());
        return njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
    }

    /**
     * 批量删除用户注册信息
     *
     * @param ids 需要删除的用户注册信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserRegisterByIds(Long[] ids)
    {
        return njtUserRegisterMapper.deleteNjtUserRegisterByIds(ids);
    }

    /**
     * 删除用户注册信息信息
     *
     * @param id 用户注册信息主键
     * @return 结果
     */
    @Override
    public int deleteNjtUserRegisterById(Long id)
    {
        return njtUserRegisterMapper.deleteNjtUserRegisterById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Boolean checkMobilePhone(String mobilePhone) {
        NjtUserRegister njtUserRegister = new NjtUserRegister();
        njtUserRegister.setMobilePhone(mobilePhone);
        return !njtUserRegisterMapper.selectNjtUserRegisterList(njtUserRegister).isEmpty();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void register(RegisterBo registerBo) {
        //1. 校验验证码
        String redisCode = stringRedisTemplate.opsForValue().get("yzm:store:"+registerBo.getMobilePhone());
        String checkCode = registerBo.getCheckCode();
        if(StringUtils.isEmpty(checkCode) || !checkCode.equals(redisCode)){
            throw new ServiceException("验证码异常");
        }
        //2. 校验手机号是否已经注册
        if (checkMobilePhone(registerBo.getMobilePhone())) {
            throw new ServiceException("手机号已经注册");
        }
        //3. 新增用户
        //3.1 新增系统用户表
        SysUser sysUser = new SysUser();
        sysUser.setPassword(SecurityUtils.encryptPassword(registerBo.getPassword()));
        sysUser.setUserName(registerBo.getMobilePhone());
        sysUser.setCreateBy(registerBo.getMobilePhone());
        sysUser.setUserType(NjtConstants.PERSON_TYPE_03);
        Date createTime = new Date();
        sysUser.setCreateTime(createTime);
        sysUserService.registerUser(sysUser);

        Long userId = sysUser.getUserId();

        //3.2 新增农检通的注册表
        NjtUserRegister njtUserRegister = new NjtUserRegister();
        njtUserRegister.setId(userId);
        njtUserRegister.setRegisterTime(createTime);
        njtUserRegister.setCreateTime(createTime);
        njtUserRegister.setMobilePhone(registerBo.getMobilePhone());
        njtUserRegister.setCreateBy(registerBo.getMobilePhone());
        njtUserRegister.setUserStatus(NjtConstants.STATUS_NORMAL);
        njtUserRegister.setDelFlag(NjtConstants.DEL_FLAG_1);
        njtUserRegister.setMemberFlag(YesOrNoEnum.NO.CODE);
        njtUserRegisterMapper.insertNjtUserRegister(njtUserRegister);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void perfect(NjtUserPerfect perfect) {
        try {
            Long userId = SecurityUtils.getUserId();
            //1.更新注册表中的完善信息
            NjtUserRegister njtUserRegister = new NjtUserRegister();
            njtUserRegister.setId(userId);
            njtUserRegister.setPerfectFlag(YesOrNoEnum.YES.CODE);
            njtUserRegister.setCompanyName(perfect.getCompanyName());
            Date perfectTime = new Date();
            njtUserRegister.setPerfectTime(perfectTime);
            njtUserRegisterMapper.updateNjtUserRegister(njtUserRegister);
            //2.新增完善信息
            perfect.setId(userId);
            perfect.setMobilePhone(SecurityUtils.getUsername());
            perfect.setRegisterTime(SecurityUtils.getLoginUser().getUser().getCreateTime());
            perfect.setCreateTime(perfectTime);
            perfect.setCreateBy(SecurityUtils.getUsername());
            njtUserPerfectMapper.insertNjtUserPerfect(perfect);
        } catch (Exception e) {
            log.error("完善用户信息异常",e);
            throw new ServiceException("完善用户信息异常");
        }
    }
}
